package elastic

import (
	"../model"
	"context"
	"encoding/json"
	"fmt"
	"github.com/olivere/elastic"
	"strconv"
	"strings"
)

func ElaToEla() {
	client, _ := ESClient()
	defer client.Stop()
	totalHits := QueryCount("coolq")
	n := 0
	for i := 0; i < 20; i++ {
		bulkRequest := client.Bulk()
		for j := 0; j < 10; j++ {
			if n >= int(totalHits) {
				break
			}
			res := QuerySearchAll("coolq", 5000, j+1)
			for _, item := range res.Hits.Hits {
				n++
				var cq model.CoolqData
				json.Unmarshal(*item.Source, &cq)
				fmt.Println(cq)
				//res, err := client.Index().Index("coolq_data").Type("group_data").Id(strconv.Itoa(n)).BodyJson(cq).Do(context.Background())
				//FailOnErr(err, "insert err ")
				//fmt.Println(res.Shards)
				userInfo := strings.ReplaceAll(cq.UserInfo, "'", "\"")
				insertData := model.Coolq{Groupid: cq.Groupid, Qq: cq.Qq, Nickname: cq.Nickname, Msg: cq.Msg, UserInfo: userInfo, CreatedAt: cq.CreatedAt}
				req := elastic.NewBulkIndexRequest().Index("coolq_data").Type("group_data").Id(strconv.Itoa(n)).Doc(insertData)
				bulkRequest = bulkRequest.Add(req)
				fmt.Println(n)
			}
		}
		bulkResponse, err := bulkRequest.Do(context.Background())
		FailOnErr(err, "bluk 插入数据失败 ")
		if bulkResponse != nil {
			fmt.Println(bulkResponse.Errors)
		}
	}
	fmt.Println(totalHits)
}
